package main

import (
	"fmt"
	"golang.org/x/net/context"
	"time"
)

func main() {
	ctx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
	defer cancelFunc()
	go task(ctx)
	time.Sleep(10 * time.Second)
}

func task(ctx context.Context) {
	select {
	case <-ctx.Done():
		fmt.Println("Task Done=>", ctx.Err())
	case <-time.After(7 * time.Second):
		fmt.Println("time.After")
	}
	fmt.Println("demo1")
	time.Sleep(5 * time.Second)
	fmt.Println("demo2")
}
